using System;
using System.Collections.Generic;
using System.Reflection;
namespace Lifter.Utility
{
    public static class SimpleCache
    {
        private static Dictionary<string, Dictionary<string, object>> objectList = new Dictionary<string, Dictionary<string, object>>();
        private static void CreateNode(string nodeID)
        {
            if (nodeID == null)
            {
                throw new NullReferenceException("DataStorage.CreateNode(string nodeID) : nodeID is NULL.");
            }
            try
            {
                Dictionary<string, object> value = new Dictionary<string, object>();
                SimpleCache.objectList.Add(nodeID, value);
            }
            catch (Exception)
            {
                throw new Exception("DataStorage.CreateNode(string nodeID) : Duplicate NodeID, NodeID = " + nodeID);
            }
        }
        public static bool IsNodeExist(string nodeID)
        {
            return SimpleCache.objectList.ContainsKey(nodeID);
        }
        public static void RemoveNode(string nodeID)
        {
            SimpleCache.objectList.Remove(nodeID);
        }
        public static int GetNodeCount()
        {
            return SimpleCache.objectList.Count;
        }
        public static int GetObjectCount(string nodeID)
        {
            Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
            int result;
            if (dictionary == null)
            {
                result = 0;
            }
            else
            {
                result = dictionary.Count;
            }
            return result;
        }
        public static object GetObject(string nodeID, string objectID)
        {
            object result;
            try
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                if (dictionary == null)
                {
                    result = null;
                    return result;
                }
                if (dictionary.Count <= 0)
                {
                    result = null;
                    return result;
                }
                foreach (KeyValuePair<string, object> current in dictionary)
                {
                    if (current.Key.Equals(objectID))
                    {
                        result = current.Value;
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                result = null;
                return result;
            }
            result = null;
            return result;
        }
        public static string[] GetObjectNodeKey(string nodeID)
        {
            Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
            string[] result;
            if (dictionary == null)
            {
                result = null;
            }
            else
            {
                string[] array = new string[dictionary.Count];
                dictionary.Keys.CopyTo(array, 0);
                result = array;
            }
            return result;
        }
        public static object[] GetObjectAll(string nodeID)
        {
            object[] array = null;
            object[] result;
            try
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                if (dictionary == null)
                {
                    result = null;
                }
                else
                {
                    if (dictionary.Count <= 0)
                    {
                        result = null;
                    }
                    else
                    {
                        int num = 0;
                        array = new object[dictionary.Count];
                        foreach (KeyValuePair<string, object> current in dictionary)
                        {
                            array[num] = current.Value;
                            num++;
                        }
                        result = array;
                    }
                }
            }
            catch (Exception)
            {
                SimpleCache.CreateNode(nodeID);
                result = array;
            }
            return result;
        }
        public static object[] GetObjectAllByValue(string nodeID)
        {
            object[] array = null;
            object[] result;
            try
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                if (dictionary == null)
                {
                    result = null;
                    return result;
                }
                if (dictionary.Count <= 0)
                {
                    result = null;
                    return result;
                }
                array = new object[dictionary.Count];
                dictionary.Values.CopyTo(array, 0);
            }
            catch (Exception)
            {
                result = null;
                return result;
            }
            result = array;
            return result;
        }
        public static object[] GetObject(string nodeID, string objectID, string query)
        {
            return null;
        }
        public static void RemoveObject(string nodeID, string objectID)
        {
            try
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                if (dictionary != null)
                {
                    if (dictionary.Count > 0)
                    {
                        foreach (KeyValuePair<string, object> current in dictionary)
                        {
                            if (current.Key.Equals(objectID))
                            {
                                dictionary.Remove(objectID);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        public static bool IsObjectExist(string nodeID, string objectID)
        {
            bool result;
            try
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                if (dictionary == null)
                {
                    result = false;
                    return result;
                }
                if (dictionary.Count <= 0)
                {
                    result = false;
                    return result;
                }
                foreach (KeyValuePair<string, object> current in dictionary)
                {
                    if (current.Key.Equals(objectID))
                    {
                        result = true;
                        return result;
                    }
                }
            }
            catch (Exception)
            {
                result = false;
                return result;
            }
            result = false;
            return result;
        }
        public static void RecoverStorage()
        {
        }
        public static void UpdateObject(string nodeID, string objectID, object objects)
        {
        }
        public static void AddObject(string nodeID, string objectID, object objectValue)
        {
            if (nodeID == null)
            {
                throw new NullReferenceException("DataStorage.AddObject(string nodeID, string objectID, Object object) : nodeID is NULL.");
            }
            if (!SimpleCache.objectList.ContainsKey(nodeID))
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                SimpleCache.objectList.Add(nodeID, dictionary);
                dictionary.Add(objectID, objectValue);
            }
            else
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                try
                {
                    dictionary.Add(objectID, objectValue);
                }
                catch (Exception)
                {
                }
            }
        }
        public static void AddObject(string nodeID, object objectValue)
        {
            string text = "";
            if (nodeID == null)
            {
                throw new NullReferenceException("DataStorage.AddObject(string nodeID, Object object) : nodeID is NULL.");
            }
            if (!SimpleCache.objectList.ContainsKey(nodeID))
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                SimpleCache.objectList.Add(nodeID, dictionary);
                Type type = objectValue.GetType();
                PropertyInfo property = type.GetProperty("ObjectID");
                if (property == null)
                {
                    throw new Exception("DataStorage.AddObject(string nodeID, Object object) : The 'object' has not a ObjectID ");
                }
                text = (string)property.GetValue(objectValue, null);
                dictionary.Add(text, objectValue);
            }
            else
            {
                Dictionary<string, object> dictionary = SimpleCache.objectList[nodeID];
                Type type = objectValue.GetType();
                PropertyInfo property = type.GetProperty("ObjectID");
                if (property == null)
                {
                    throw new Exception("DataStorage.AddObject(string nodeID, Object object) : The 'object' has not a ObjectID ");
                }
                text = (string)property.GetValue(objectValue, null);
                try
                {
                    dictionary.Add(text, objectValue);
                }
                catch (Exception)
                {
                    throw new Exception("DataStorage.AddObject(string nodeID, Object object) : Duplicate ObjectID, ObjectID = " + text);
                }
            }
        }
    }
}
